let https = require('https')
let User = require('../../database/UserSchema')
module.exports = async (req, res) => {
	data = JSON.parse(req.body.data)
	let uid = await User.findOne({
		"username": data.userid
	})
	if (uid) {
		res.send(false)
	} else {
		let options = {
			host: 'aip.baidubce.com',
			path: '/rest/2.0/face/v3/faceset/user/add?access_token="' + access_token + '"',
			method: 'POST',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			}
		}

		let contents = JSON.stringify({
			image: data.dataurl.replace(/\s/g, "+").replace(
				/^data:image\/\w+\Wbase64,/, ""),
			image_type: "BASE64",
			group_id: "test",
			user_id: data.userid
		})
		let req_baidu = https.request(options, function(res_baidu) {
			res_baidu.setEncoding('utf8')
			res_baidu.on('data', function(chunk) {
				res.send(chunk)
				if (JSON.parse(chunk).error_code == 0) {
					User.create({
						"username": data.userid,
						"password": data.password
					})
				}
			})
		})
		req_baidu.write(contents)
		req_baidu.end()
	}
}
